#---- begin snakebids boilerplate ----------------------------------------------

import snakebids
from snakebids import bids

configfile: workflow.source_path('../config/snakebids.yml')

# Get input wildcards
inputs = snakebids.generate_inputs(
    bids_dir=config["bids_dir"],
    pybids_inputs=config["pybids_inputs"],
    pybids_database_dir=config.get("pybids_db_dir"),
    pybids_reset_database=config.get("pybids_db_reset"),
    derivatives=config.get("derivatives", None),
    participant_label=config.get("participant_label", None),
    exclude_participant_label=config.get("exclude_participant_label", None),
    use_bids_inputs=True,
)



#this adds constraints to the bids naming
wildcard_constraints:  **snakebids.get_wildcard_constraints(config['pybids_inputs'])

#---- end snakebids boilerplate ------------------------------------------------


rule smooth:
    input: inputs.path['bold']
    output:
        bids(
            root=config['root'],
            datatype='func',
            desc='smooth{fwhm}mm',
            suffix='bold.nii.gz',
            **inputs.wildcards['bold']
        )
    container: config['singularity']['fsl']
    log:
        bids(
            root='logs',
            suffix='smooth.log',
            fwhm='{fwhm}',
            **inputs.wildcards['bold']
        )
    params: sigma = lambda wildcards: f'{float(wildcards.fwhm)/2.355:0.2f}'
    shell: 'fslmaths {input} -s {params.sigma} {output}'


rule all:
    input:
        expand(
            expand(
                rules.smooth.output,
                fwhm = config['smoothing_fwhm'],
                allow_missing=True,
            ),
            zip,
            **inputs.zip_lists['bold']
        )
    default_target: True
